package com.ruoyi.web;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.framework.web.service.MyLoginService;
import com.ruoyi.framework.web.service.MyTokenService;
import com.ruoyi.system.domain.Hr;
import com.ruoyi.system.domain.Seeker;
import com.ruoyi.system.domain.SeekerPdfRelation;
import com.ruoyi.system.domain.Upfile;
import com.ruoyi.system.service.IJobSeekerRelationService;
import com.ruoyi.system.service.ISeekerPdfRelationService;
import com.ruoyi.system.service.ISeekerService;
import com.ruoyi.system.service.IUpfileService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@RestController
public class EmployeeController extends BaseController {
    @Autowired
    private ISeekerService seekerService;
    @Autowired
    private MyLoginService myLoginService;
    @Autowired
    private MyTokenService myTokenService;
    @Autowired
    private ISeekerPdfRelationService seekerPdfRelationService;
    @Autowired
    private IUpfileService upfileService;
    @RequestMapping("/employeesignup")
    public AjaxResult employeesignup(@RequestParam String seekergender,@RequestParam String phone,@RequestParam String pwd,@RequestParam String signupname,@RequestParam String seekeremail,@RequestParam String seekerdate,@RequestParam String seekerisedu) throws ParseException {
        Seeker temseker = seekerService.selectSeekerBySeekerTel(phone);
        if(temseker!=null){//存在注册失败
            new AjaxResult(1,"已经存在该求职者账号,请直接登录");
        }
        System.out.println("进入求职者注册");
        Seeker seeker=new Seeker();
        seeker.setSeekerName(signupname);
        seeker.setSeekerTel(phone);
        seeker.setSeekerPwd(pwd);
        seeker.setSeekerEmail(seekeremail);
        seeker.setSeekerIsfreshman(seekerisedu);
        seeker.setSeekerGender(seekergender);
        //String转date
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        seeker.setSeekerDob(ft.parse(seekerdate));
        return toAjax(seekerService.insertSeeker(seeker));
    }
    @RequestMapping("/seekerdetail")
    public AjaxResult seekerdetail(HttpServletRequest request, HttpServletResponse response){
        String token = request.getParameter("token");
        Long seekerid = myTokenService.getloginid(token);
        Seeker seeker=seekerService.selectSeekerBySeekerId(seekerid);
        SeekerPdfRelation seekerPdfRelation = seekerPdfRelationService.selectSeekerPdfRelationBySeekerId(seekerid);
        Upfile upfile =null;
        if(seekerPdfRelation!=null) {
            upfile = upfileService.selectUpfileByFileId(seekerPdfRelation.getPdfId());
        }
        AjaxResult ajax = AjaxResult.success();

        ajax.put("seeker",seeker);
        ajax.put("upfile",upfile);
        return ajax;

    }
    @RequestMapping("/updateseekerdetail")
    public AjaxResult updateseekerdetail(HttpServletRequest request, HttpServletResponse response) throws ParseException, UnsupportedEncodingException {
        String btn = request.getParameter("btn");
        String modify_data = request.getParameter("data");
        System.out.println(btn);
        System.out.println(modify_data);
        Seeker seeker = new Seeker();
        Map<String,String> modify=myTokenService.doparse(modify_data);
        seeker.setSeekerId(Long.valueOf(modify.get("seekerbid")));
        //工作期望修改
        if(btn.equals("seekerexpworkform")){
            if(modify.get("expworktype")!=null){
            seeker.setSeekerExpjobtype(modify.get("expworktype"));}
            if(modify.get("exppos")!=null){
            seeker.setSeekerExppos(modify.get("exppos"));}
            if(modify.get("expworkloc")!=null){
            seeker.setSeekerExpworkloc(modify.get("expworkloc"));}
            if(modify.get("expworkind")!=null){
            seeker.setSeekerExpind(modify.get("expworkind"));}
            if(modify.get("advantage")!=null){
            seeker.setSeekerAdv(modify.get("advantage"));}
            if(modify.get("expworsal")!=null){
            seeker.setSeekerExpsal(modify.get("expworsal"));}

        }
        //教育经历修改
        else if(btn.equals("educationexp")){
            if(modify.get("edulvl")!=null){
            seeker.setSeekerEdulvl(modify.get("edulvl"));}
            if(modify.get("school")!=null){
            seeker.setSeekerSchname(modify.get("school"));}
            if(modify.get("schoolperiod")!=null){
            seeker.setSeekerStudyper(modify.get("schoolperiod"));}
            if(modify.get("major")!=null){
            seeker.setSeekerMaj(modify.get("major"));}
            if(modify.get("currcity")!=null){
            seeker.setSeekerCity(modify.get("currcity"));}
        }
        //基本信息修改
        else if(btn.equals("seekerinfodetail")){
            seeker.setSeekerName(modify.get("seekername"));
            seeker.setSeekerTel(modify.get("seekertel"));
            seeker.setSeekerEmail(modify.get("seekeremail"));
            seeker.setSeekerGender(modify.get("seekergender"));
            seeker.setSeekerIsfreshman(modify.get("seekerisedu"));
            seeker.setSeekerPwd(modify.get("seekerpwd"));
            seeker.setSeekerDob(myTokenService.str2date(modify.get("seekerdate")));//要转为date形式
        }
        return toAjax(seekerService.updateSeeker(seeker));
    }

}
